﻿define('ui/controller/controlLevelController',
	['local/cache', 'api/controlLevelService',
	 'ui/controller/conditionController'],
	function(cache, controlLevelService, conditionController) {

	var controlLevelController = {
			
		initial: function(){
			$("em.cl-type>span").first().text('监管类型');
			
			if(cache.getControlLevelData() == null) {
				cache.setControlLevelData(controlLevelService.getControleLevels());
				controlLevelController.render(cache.getControlLevelData());
			}
		},
		
		render: function(data){
			
			AMapUI.loadUI(['misc/MarkerList'], function(MarkerList) {
				var all = '<li id="cl-0" data-val="business_enterprise_flag=1;code=;custom=business_area:监管类型">'+
								'<span class="cl-area-name">'+
									'全部'+
								'</span>'+
								'<span class="cl-right-arrow">'+
								'</span>'+
						  '</li>';
				
				var typeList =  '<% for(var i=0;i < data.length;i++) { %>'+  
									'<li id="cl-<%- i %>" data-val="business_enterprise_flag=1;code=<%- data[i].generalCode %>;custom=business_area:<%- data[i].cateGoryName %>">'+
										'<span class="cl-area-name">'+
											'<%- data[i].cateGoryName %>'+
										'</span>'+
										'<span class="cl-right-arrow">'+
										'</span>'+
									'</li>'+
								'<% } %>';
				var html = '<ul class="classify-sort-type">'+
							all+
							MarkerList.utils.template(typeList, {data: data})+
							'</ul>';  
				$("#type").html("").html(html); 
				$('#type ul.classify-sort-type li').first().addClass('sort-select');
				
				controlLevelController.onMove();
				controlLevelController.onSelect();
			});
		},
		
		onMove: function(item) {
			$('#type ul.classify-sort-type li').each(function(){
				$(this).mouseover(function(){
					$('ul.classify-sort-type li').removeClass('sort-select');
					$(this).addClass('sort-select');
				});
			});
		},
		
		onSelect: function(item) {
			$('#type ul.classify-sort-type li').each(function(){
				$(this).click(function(){
					var $span = $("em.cl-type>span").first();
					var val = $(this).attr('data-val');
					var s = val.split(";")
					if($(this).text()=='全部') {
						if(s && s.length>0){
							$span.text(s[2].split(":")[1]);
							conditionController.setControlLevelCode($span, s[1].split("=")[1]?s[1].split("=")[1]:'');
						}
					}else {
						$span.text($(this).text());
						conditionController.setControlLevelCode($span, s[1].split("=")[1]?s[1].split("=")[1]:'');
					}
					$('#type ul.classify-sort-type li').removeClass('sort-select');
					$(this).addClass('sort-select');
					conditionController.hideConditionList();
					
					//todo：enterprise
					var enterpriseML = require('ui/markerList/enterpriseML');
					enterpriseML.reRenderData();
				});
			});
		}
	};
	
	return controlLevelController;
});